package com.hyjx.framework.component.excel.column;

import com.alibaba.fastjson.JSONArray;
import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.component.excel.template.ExcelTemplate;
import com.hyjx.framework.component.excel.template.ExcelTemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * excel 模板列控制器
 *
 * @author 1
 */
@Controller
@RequestMapping("/exceltemplatecolumn")
public class ExcelTemplateColumnController {

    @Resource
    private ExcelTemplateColumnService excelTemplateColumnService;
    @Resource
    private ExcelTemplateService excelTemplateService;

    /**
     * 查询模板列
     *
     * @param etc
     * @return
     */
    @RequestMapping(value = "/query", produces = "application/json; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String query(ExcelTemplateColumn etc) {
        try {
            String resultStr = this.excelTemplateColumnService.queryAll(etc);
            return resultStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 打开新增模板列页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoAdd", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ModelAndView gotoAdd() {
        ModelAndView mav = new ModelAndView("/framework/exceltemplatecolumn/add");
        return mav;
    }

    /**
     * 新增模板列
     *
     * @param etc
     * @return
     */
    @RequestMapping(value = "/doAdd", produces = "text/html; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String doAdd(ExcelTemplateColumn etc) {
        try {
            etc.setPub_excel_template_column_id(CommonUtil.getUUID32());
            this.excelTemplateColumnService.insert(etc);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 打开模板列页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoExcelTemplateColumn", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoList() {
        return "/framework/exceltemplatecolumn/list";
    }

    /**
     * 打开模板列编辑页面
     *
     * @param etc
     * @return
     */
    @RequestMapping(value = "/gotoEdit", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ModelAndView gotoEdit(ExcelTemplateColumn etc) {
        etc = this.excelTemplateColumnService.queryById(etc);
        ModelAndView mav = new ModelAndView("/framework/exceltemplatecolumn/edit");
        mav.addObject("etcModel", etc);
        return mav;
    }

    /**
     * 编辑模板列
     *
     * @param etc
     * @return
     */
    @RequestMapping(value = "/doEdit", produces = "text/html; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String doEdit(ExcelTemplateColumn etc) {
        try {
            this.excelTemplateColumnService.update(etc);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }

    }

    /**
     * 查看模板列
     *
     * @param etc
     * @return
     */
    @RequestMapping(value = "/gotoView", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ModelAndView gotoView(ExcelTemplateColumn etc) {
        etc = this.excelTemplateColumnService.queryById(etc);
        ModelAndView mav = new ModelAndView("/framework/exceltemplatecolumn/view");
        mav.addObject("etcModel", etc);
        return mav;
    }


    /**
     * 删除模板列
     *
     * @param etc
     * @return
     */
    @RequestMapping(value = "/doDelete", produces = "text/html; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String doDelete(ExcelTemplateColumn etc) {
        try {
            this.excelTemplateColumnService.detele(etc);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }

    }

    /**
     * 获取对应的模板
     *
     * @return
     */
    @RequestMapping(value = "/getExcelTemplates", produces = "text/html; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String getExcelTemplate() {

        List<ExcelTemplate> list = this.excelTemplateService.queryList(null);
        List<Map<String, String>> valueList = new ArrayList<Map<String, String>>();
        for (ExcelTemplate et : list) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("exceltemplate_id", et.getPub_excel_template_id());
            map.put("exceltemplate_value", et.getModule_ch_name());
            valueList.add(map);
        }

        String resultStr = JSONArray.toJSONString(valueList);
        return resultStr;
    }

    /**
     * 获取模板列对应模板的英文名字
     *
     * @param et
     * @return
     */
    @RequestMapping(value = "/getExcelTemplatesName", produces = "text/html; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String getExcelTemplateName(ExcelTemplate et) {

        List<ExcelTemplate> list = this.excelTemplateService.queryList(et);
        String resultStr = null;
        if (list != null)
            resultStr = list.get(0).getModule_en_name();
        return resultStr;
    }
}
